package com.oa.service.impl;

import com.oa.bean.Dept;
import com.oa.bean.Emp;
import com.oa.utils.PageBean;
import com.oa.dao.DeptDao;
import com.oa.service.DeptService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public PageBean pageQuery(PageBean pageBean) {
        return deptDao.pagingQuery(pageBean);
    }

    @Override
    public void save(Dept model) {
        deptDao.save(model);
    }

    @Override
    public Dept checkDept(String name) {
        return deptDao.checkDept(name);
    }


    public String delete(Dept model) {

        //删除部门钱确定部门下员工有无未交接的客户
        Dept dept = deptDao.findById(model.getId());

        if (dept == null) {
            return "failed";
        }
        Set<Emp> emps = dept.getEmps();

        boolean canDel = true;
        for (Emp emp : emps) {
            if (emp.getCustomers().size() > 0) {
                canDel = false;
            }
            emp.setDept(null);
            emp.setIs(0);
        }
        if (canDel) {

            dept.setEmps(null);

            dept.setIs(0);

            deptDao.update(dept);

        } else {
            return "failed";
        }
        return "success";
    }

    @Override
    public void saveOrUpdate(Dept dept) {
        deptDao.saveOrUpdate(dept);
    }

    @Override
    public Dept findDeptById(Integer id) {
        return deptDao.findById(id);
    }

    @Override
    public List<Dept> findAll() {

        return deptDao.findAll();
    }

    @Override
    public List<String> checkCanDeleted(Integer id) {
        List<String> list = new ArrayList<>();

        Dept dept = deptDao.findById(id);

        Set<Emp> emps = dept.getEmps();

        for (Emp emp : emps) {
            if (emp.getCustomers().size() > 0) {
                list.add(emp.getTruename());
            }
        }

        return list;
    }

    @Override
    public void restoreDept(Integer id) {

        Dept dept = deptDao.findById(id);

        dept.setIs(1);

        deptDao.update(dept);

    }

    @Override
    public boolean checkFlag(Integer id) {
        Dept dept = deptDao.findById(id);

        Set<Emp> emps = dept.getEmps();

        boolean is = false;

        for (Emp emp : emps) {
            Integer flag = emp.getFlag();
            if (flag == 1) {
                is = true;
            }
        }

        return is;
    }

    @Override
    public List<Dept> findAllDeptNames() {

        DetachedCriteria dc = DetachedCriteria.forClass(Dept.class);
        dc.add(Restrictions.eq("is", 1));

        List<Dept> list = deptDao.findByCriteriaReturnList(dc);

        return list;
    }


}
